﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

public partial class CustomerManagement_CREDITTYPEEDIT : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }

    private string Code
    {
        get
        {
            if (Request.QueryString["code"] != null)
            {
                return Request.QueryString["code"].ToString();
            }
            else
            {
                return "";
            }
        }
    }

    private Model.CREDITTYPE CreditType
    {
        get
        {
            Model.CREDITTYPE creditType = null;
            if (ViewState["CreditType"] != null)
            {
                creditType = (Model.CREDITTYPE)ViewState["CreditType"];
            }
            return creditType;
        }
        set
        {
            ViewState["CreditType"] = value;
        }
    }
    private bool IsAdd
    {
        get
        {
            return Convert.ToBoolean(ViewState["IsAdd"]);
        }
        set
        {
            ViewState["IsAdd"] = value;
        }

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        //Check.CheckPermission("200300104");
        if (!IsPostBack)
        {
            if (this.Code != "")
            {
                this.CreditType = new BLL.CREDITTYPE().GetModel(int.Parse(this.Code));
                if (this.CreditType != null)
                {
                    txtType.Text = CreditType.CREDITTYPE_NAME;
                    txtFS.Text = CreditType.CREDITTYPE_FS.ToString();
                    this.IsAdd = false;
                    btnAddOrUpdate.Text = "更新";
                }
                else
                {
                    Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('系统中无此记录!');location.href='CREDITTYPE.aspx';</script>");
                }
            }
            else
            {
                this.IsAdd = true;
                btnAddOrUpdate.Text = "添加";
            }
            btnCancel.Attributes.Add("onclick", "if(!confirm('要取消" + btnAddOrUpdate.Text + "并返回上一页吗?'))return false;");
        }

    }
    protected void btnAddOrUpdate_Click(object sender, EventArgs e)
    {
        if (txtType.Text.Length < 1)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('信誉度调整类型不能为空')", true);
            return;
        }
        if (!CheckNum(txtFS.Text.Trim()))
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('调整分数必须是浮点数')", true);
            return;
        }
        if (this.IsAdd == true)
        {
            if (true)//Check.CheckPermission("200300104", "00001")
            {
                Model.CREDITTYPE creditType = new Model.CREDITTYPE();
                creditType.CREDITTYPE_NAME = txtType.Text.Trim();
                creditType.CREDITTYPE_FS = decimal.Parse(txtFS.Text.Trim());
                if (new BLL.CREDITTYPE().Add(creditType) > 0)
                {
                    //Share.InsertLog("200300104", "00001");
                    Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('添加记录成功');location.href='CREDITTYPE.aspx';</script>");
                }
                else
                {
                    Share.Alert(this.Page, "添加记录失败,请重新添加。");
                }

            }
            else
            {
                Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('无权进行此操作');location.href='CREDITTYPE.aspx';</script>");
            }
        }
        else
        {
            if (true)//Check.CheckPermission("200300104", "00002")
            {
                CreditType.CREDITTYPE_NAME = txtType.Text.Trim();
                CreditType.CREDITTYPE_FS = decimal.Parse(txtFS.Text.Trim());

                if (new BLL.CREDITTYPE().Update(this.CreditType))
                {
                    //Share.InsertLog("200300104", "00002");
                    Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('更新记录成功');location.href='CREDITTYPE.aspx';</script>");
                }
                else
                {
                    Share.Alert(this.Page, "更新记录失败,请重新编辑更新。");
                }
            }
            else
            {
                Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('无权进行此操作');location.href='CREDITTYPE.aspx';</script>");
            }

        }

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {

        Response.Redirect("CREDITTYPE.aspx");
    }
    private bool CheckNum(string str)
    {
        Regex reg = new Regex(@"^(-?\d+)(\.\d+)?$");
        if (reg.IsMatch(str))
        {
            return true;
        }
        return false;
    }
}
